package com.quick.im.common.base;

import com.quick.im.common.enums.GlobalExceptionEnum;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;

import java.util.List;
import java.util.Optional;

/**
 * @program: travel
 * @description:
 * @author: Mr.si
 * @create: 2022-08-03 15:25
 **/
@Data
@NoArgsConstructor
@Builder
public class BaseResponse<T> {

    private T data;

    private String code;

    private String desc;

    private Integer pageNo;

    private Integer pageSize;

    private Long total;

    /**
     * 消息未读数
     */
    private Integer unreadTotal;


    public static <T> BaseResponse<T> success() {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(GlobalExceptionEnum.SUCCESS.getCode());
        baseResponse.setDesc(GlobalExceptionEnum.SUCCESS.getResult());
        return baseResponse;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(GlobalExceptionEnum.SUCCESS.getCode());
        baseResponse.setDesc(GlobalExceptionEnum.SUCCESS.getResult());
        baseResponse.setData(data);
        return baseResponse;
    }

//    public static <T> BaseResponse<T> successLimit(PageInfo pageInfo, T data) {
//        return new BaseResponse<T>(data,ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getDesc(),pageInfo.getPageNum(),pageInfo.getPageSize(), pageInfo.getTotal());
//    }

//    public static <T> BaseResponse<T> successLimit(PageInfo pageInfo, T data, Integer unreadTotal) {
//        return new BaseResponse<T>(data,ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getDesc(),pageInfo.getPageNum(),pageInfo.getPageSize(), pageInfo.getTotal(), unreadTotal );
//    }

    public static <T> BaseResponse<T> fail() {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(GlobalExceptionEnum.FAIL.getCode());
        baseResponse.setDesc(GlobalExceptionEnum.FAIL.getResult());
        return baseResponse;
    }

    public static <T> BaseResponse<T> fail(String result) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(GlobalExceptionEnum.FAIL.getCode());
        baseResponse.setDesc(result);
        return baseResponse;
    }

//    public static <T> BaseResponse<T> tokenExpired(String message){
//        BaseResponse baseResponse = new BaseResponse();
//        baseResponse.setCode(ResponseCodeEnum.TOKEN_EXPIRED.getCode());
//        baseResponse.setDesc(message);
//        return baseResponse;
//    }

    public BaseResponse(T data) {
        this.data = data;
        this.code = GlobalExceptionEnum.SUCCESS.getCode();
        this.desc = GlobalExceptionEnum.SUCCESS.getResult();
    }

    public BaseResponse(T data, String code, String desc, Integer pageNo, Integer pageSize, Long total, Integer unreadTotal) {
        this.data = data;
        this.code = code;
        this.desc = desc;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
        this.unreadTotal = unreadTotal;
    }

    public BaseResponse(T data, String code, String desc, Integer pageNo, Integer pageSize, Long total) {
        this.data = data;
        this.code = code;
        this.desc = desc;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
    }


    public static void isNull(@Nullable Object obj, String msg) {

        if (obj instanceof Optional) {
            if (!((Optional) obj).isPresent()) {
                throw new IllegalArgumentException(msg);
            }
            return;
        }
        if (obj instanceof List) {
            if (((List<?>) obj).size() == 0) {
                throw new IllegalArgumentException(msg);
            }
            return;
        }
        if (obj == null || "".equals(obj)) {
            throw new IllegalArgumentException(msg);
        }
    }
}
